using System.Threading;
using System.Threading.Tasks;
using MediatR;
using B.S.BasicData.EroorCode;
using B.S.BasicData.Domain;
using B.S.BasicData.Infrature;
using System.Collections.Generic;
using System.Linq;

public class ProindexinfoBatchDeleteHandler : IRequestHandler<ProindexinfoBatchDeleteRequest, ApiResult<int>>
{
    private readonly IBaseRepository<Proindexinfo> _repository;
    public ProindexinfoBatchDeleteHandler(IBaseRepository<Proindexinfo> repository)
    {
        _repository = repository;
    }

    public async Task<ApiResult<int>> Handle(ProindexinfoBatchDeleteRequest request, CancellationToken cancellationToken)
    {
        if (request.Ids == null || !request.Ids.Any())
            return ApiResult<int>.Fail("未指定要删除的ID");

        var all = (await _repository.GetAllAsync()).Where(x => request.Ids.Contains(x.Id)).ToList();
        if (!all.Any())
            return ApiResult<int>.Fail("未找到要删除的数据");

        int count = 0;
        foreach (var item in all)
        {
            item.IsDeleted = true;
            await _repository.UpdateAsync(item);
            count++;
        }
        return ApiResult<int>.Success(count, $"成功逻辑删除{count}条数据");
    }
} 